//
//  SISSTenantDetailViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2017/12/15.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSTenantDetailViewController.h"
#import "SISSTenantView.h"
#import "SISSRecommendRentOutViewController.h"
#import "SISSMorePopupView.h"
#import "SISSNetworkingManager+RentOutProgress.h"

@interface SISSTenantDetailViewController ()<SISSMorePopupViewDelegate>

/* tenantView */
@property (weak, nonatomic) SISSTenantView *tenantView;
/* 推荐按钮 */
@property (weak, nonatomic) UIButton *recommendButton;

@end

@implementation SISSTenantDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加右侧分享投诉按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"more..."] style:UIBarButtonItemStylePlain target:self action:@selector(shareAndComplainClick)];
}

- (void)setAlbpId:(NSString *)albpId {
    _albpId = albpId;
    
    WeakSelf
    [kNetworkManager requestRentOutRecommendTenantWithAlbpId:albpId success:^(SISSTenantModel *tenantModel) {
        weakSelf.noDataShowView.hidden = tenantModel != nil;
        weakSelf.scrollView.hidden = !tenantModel;
        weakSelf.tenantModel = tenantModel;
        weakSelf.tenantView.tenantModel = tenantModel;
        
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark - SISSMorePopupViewDelegate
- (void)morePopupViewSelectedWithIndex:(NSInteger)index {
    
    if (index == 0) {
        
        NSString *genderStr = self.tenantModel.sex == 2 ? @"女士" : @"先生";
        NSString *title = [NSString stringWithFormat:@"%@%@ 求租",[self.tenantModel.renterName substringToIndex:1],genderStr];
        NSString *descr = [NSString stringWithFormat:@"%@-%@ | %@-%@ | %@",self.tenantModel.minPrice,self.tenantModel.maxPrice,self.tenantModel.region,self.tenantModel.regionDetail,self.tenantModel.houseType];
        NSString *webpageUrl = @"";
        if (self.tenantModel.rentType == 1) { // 线上
            webpageUrl = [NSString stringWithFormat:@"%@/#!/rentQuestDetail/%@",WEB_BASE_URL,self.tenantModel.ID];
        } else { // 线下
            webpageUrl = [NSString stringWithFormat:@"%@/#!/rentQuestOfflineDetail/%@",WEB_BASE_URL,self.tenantModel.ID];
        }
        
        [[SISSPublicSingleton shared] shareWithTitle:title descr:descr webpageUrl:webpageUrl currentViewController:self thumImage:nil];
    } else {
    
            SISSReportViewController *vc = [[SISSReportViewController alloc] init];
            vc.tenantModel = self.tenantModel;
            [self.navigationController pushViewController:vc animated:YES];
        }
    
}

#pragma mark - 点击事件
/**
 分享投诉按钮事件
 */
- (void)shareAndComplainClick {
    
    SISSMorePopupView *moreView = [[SISSMorePopupView alloc] initWithTitles:@[@"分享", @"举报"]];
    moreView.delegate = self;
    [moreView show];
}

/**
 点击推荐房源按钮
 */
- (void)recommendButtonClickAction:(UIButton *)sender {
    
    SISSRecommendRentOutViewController *vc = [[SISSRecommendRentOutViewController alloc] init];
    vc.isFind = self.isFind;
    vc.isDetailIn = YES;
    vc.arrId = self.tenantModel.ID;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    SISSTenantView *tenantView = [[SISSTenantView alloc] init];
    tenantView.tenantModel = self.tenantModel;
    [self.containerView addSubview:tenantView];
    self.tenantView = tenantView;
    
    // 选择按钮
    UIButton *recommendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [recommendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [recommendButton setBackgroundColor:MAIN_COLOR];
    [recommendButton setTitle:@"推荐租客" forState:UIControlStateNormal];
    [recommendButton addTarget:self action:@selector(recommendButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    recommendButton.hidden = self.isHiddenBottomButton;
    [self.view addSubview:recommendButton];
    self.recommendButton = recommendButton;
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    CGFloat buttonHeight = 44;
    [self.tenantView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(weakSelf.containerView);
        make.bottom.equalTo(weakSelf.containerView).offset(-buttonHeight);
    }];
    
    [self.recommendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(weakSelf.view);
        make.height.mas_equalTo(buttonHeight);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
